<?php

class MenuController extends Controller
{	
	public $layout='/layouts/column2';
	public $mainMenu='usuarios_grupos';
	
	public function filters()
	{
		return array(
				'accessControl',
		);
	}
	
	public function accessRules()
	{
		return array(
				array('allow',
						'actions'=>array('index'),
						'roles'=>array('ADMINISTRADOR'),
				),
				array('deny',  // deny all users
						'users'=>array('*'),
				),
		);
	}

	public function actionIndex()
	{		
		if(isset($_REQUEST['_method'])){
			$method=$_REQUEST['_method'];
			$this->procesarREST(strtolower($method));
		}else{
			if(isset($_REQUEST['model'])){
				if(Yii::app()->request->isPostRequest){
					$this->procesarREST('post');
				}
			}else{
				$this->render('index');
			}
			
		}		
	}
	
	
	public function procesarREST($method){
		if($method!="get"&&$method!='delete'){
			$model=json_decode($_REQUEST['model'],true);
		}		
		switch ($method){
			case 'get':
				  if(isset($_REQUEST['id_padre'])){
				  	$id_padre=$_REQUEST['id_padre'];
				  	$menusHijos=Menu::model()->buscarHijos($id_padre);
				  	echo json_encode($menusHijos);
				  }
				break;
			case 'post':
				  $menu=new  Menu();
				  $menu->attributes=$model;		
				  $menu->save();		
				break;
			case 'put':
				var_dump($model);
				Menu::model()->updateByPk($model['id'],$model);
				break;
			case 'delete':					
					Menu::model()->deleteByPk($_REQUEST['id']);
				break;
		}
		Yii::app()->end();
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}